//
//  CameraPhotoBehavior.m
//  BadBoy
//
//  Created by 余勇 on 2017/3/8.
//  Copyright © 2017年 niusaibing. All rights reserved.
//

#import "CameraPhotoBehavior.h"
#import <UIKit/UIKit.h>
#import <MobileCoreServices/MobileCoreServices.h>
#import "LPActionSheet.h"

typedef NS_ENUM(NSUInteger, BehaviorType) {
    BehaviorTypeCamera,
    BehaviorTypePhoto,
    BehaviorTypeTakePicture,
    BehaviorTypeTakeLetterVideo
};

@interface CameraPhotoBehavior ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (nonatomic, copy)void (^finishBlock)(UIImage *image);

/**
 创建一个操作相机/相册的行为
 
 @param type 区分是相机还是相册
 @param targetVC 调用vc
 @param finishBlock 获取到图片之后的block
 */
- (void)startBehaviorWithType:(BehaviorType)type targetVC:(UIViewController *)targetVC finishBlock:(void(^)(UIImage *image))finishBlock;

@end

@implementation CameraPhotoBehavior
+ (instancetype)behavior{
    return [[CameraPhotoBehavior alloc] init];
}

- (void)getPhotoWithtargetVC:(UIViewController *)targetVC FinishBlock:(void (^)(UIImage *image))finishBlock{
    if (targetVC == nil) {
        targetVC = [UIApplication sharedApplication].keyWindow.rootViewController;
    }
    
    [targetVC.view alertWithTitle:nil message:nil type:AlertTypeSheet cancleTitle:ASLocalizedString(@"取消")otherTitle:@[ASLocalizedString(@"从相册选取"), ASLocalizedString(@"重新拍一张")] clickBlock:^(NSInteger index) {
        if (index > 0) {
            BehaviorType currentType = BehaviorTypePhoto;
            if (index == 1){
                // BDLog(ASLocalizedString(@"点击了相册"));
                currentType = BehaviorTypePhoto;
            }else if (index == 2){
                currentType = BehaviorTypeCamera;
            }
            [self startBehaviorWithType:currentType targetVC:targetVC finishBlock:finishBlock];
        }
    }];
}

- (void)takePictureWithtargetVC:(UIViewController *)targetVC FinishBlock:(void (^)(UIImage *))finishBlock{
    [self startBehaviorWithType:BehaviorTypeTakePicture targetVC:targetVC finishBlock:finishBlock];
}

- (void)takeVideoWithtargetVC:(UIViewController *)targetVC FinishBlock:(void (^)(UIImage *))finishBlock{
    [self startBehaviorWithType:BehaviorTypeTakeLetterVideo targetVC:targetVC finishBlock:finishBlock];
}

- (void)startBehaviorWithType:(BehaviorType)type
                         targetVC:(UIViewController *)targetVC
                      finishBlock:(void (^)(UIImage *image))finishBlock{
    //保存block
    self.finishBlock = finishBlock;
    //获取设置过的控件
    UIImagePickerController *imagePickView = [self imagePickerControllerWithBehaviorType:type];
    //展示控件
    [targetVC presentViewController:imagePickView animated:YES completion:nil];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
}

/**
 创建UIImagePickerController控件并根据type进行设置

 @param type 相机操作还是相册操作
 @return 控件
 */
- (UIImagePickerController *)imagePickerControllerWithBehaviorType:(BehaviorType)type{
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
//    imagePickerController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    imagePickerController.allowsEditing = YES;
    
    if (type == BehaviorTypePhoto) {
        imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }else{
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {        //判断当前设备的系统是否是ios7.0以上
            imagePickerController.edgesForExtendedLayout = UIRectEdgeNone;
        }
        imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        //录制视频时长，默认10s
        imagePickerController.videoMaximumDuration = 15;
        
        //相机类型（拍照、录像...）字符串需要做相应的类型转换
        if (type == BehaviorTypeTakeLetterVideo) {
            imagePickerController.mediaTypes = @[(NSString *)kUTTypeMovie];
        }else{
            imagePickerController.mediaTypes = @[(NSString *)kUTTypeImage];
        }
//        imagePickerController.mediaTypes = @[(NSString *)kUTTypeMovie, (NSString *)kUTTypeImage];
        imagePickerController.videoQuality = UIImagePickerControllerQualityTypeHigh;
        
        //设置摄像头模式（拍照，录制视频）为录像模式
        imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
    }
    return imagePickerController;
}

//结果回调
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    [picker dismissViewControllerAnimated:YES completion:nil];
    NSString *mediaType=[info objectForKey:UIImagePickerControllerMediaType];
    //判断资源类型
    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]){
        //如果是图片
        UIImage *image = info[UIImagePickerControllerEditedImage];

        self.finishBlock(image);
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    }
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}

@end
